﻿
Partial Class AdminNewOrders
    Inherits Page

    Public Torders As List(Of torders)
    Public Total As Integer
    Public Cities As List(Of cities)

    Public PeriodFrom As String
    Public PeriodTo As String

    Public PeriodFromDate As DateTime
    Public PeriodToDate As DateTime

    Public Cityid As Guid = Guid.Empty

    Protected Overrides Sub OnLoad(e As EventArgs)
        MyBase.OnLoad(e)

        PeriodFromDate = Date.MinValue
        PeriodToDate = Date.MaxValue

        If Request.Form.HasKeys() Then
            Cityid = Guid.Parse(Request.Form("cityid").ToString())
            PeriodFrom = Request.Form("period_from")
            PeriodTo = Request.Form("period_to")
        End If

        If Request.QueryString.Keys.Count > 0 Then
            Try
                Cityid = Guid.Parse(Request.QueryString("cityid").ToString())
                PeriodFrom = Request.QueryString("period_from")
                PeriodTo = Request.QueryString("period_to")
            Catch ex As Exception

            End Try
        End If

        If String.IsNullOrEmpty(PeriodFrom) <> True Then
            PeriodFromDate = New Date(CType(PeriodFrom.Split(".")(2), Integer), CType(PeriodFrom.Split(".")(1), Integer), CType(PeriodFrom.Split(".")(0), Integer))
        End If

        If String.IsNullOrEmpty(PeriodTo) <> True Then
            PeriodToDate = New Date(CType(PeriodTo.Split(".")(2), Integer), CType(PeriodTo.Split(".")(1), Integer), CType(PeriodTo.Split(".")(0), Integer))
        End If

        Using context As New TaxiEntities()
            Total = context.torders.Include("taxist").Include("orders").Count(CType(Function(a) (a.torder_status > 6 And PeriodFromDate <= a.torder_created And a.torder_created <= PeriodToDate) And (Cityid = Guid.Empty Or a.taxist.city_id = Cityid), Func(Of torders, Boolean)))
            Torders = context.torders.Include("taxist").Include("orders").Include("taxist.cities").Where(CType(Function(a) (a.torder_status > 6 And PeriodFromDate <= a.torder_created And a.torder_created <= PeriodToDate) And (Cityid = Guid.Empty Or a.taxist.city_id = Cityid), Func(Of torders, Boolean))).OrderByDescending(Function(a) a.torder_created).ToList()
            Cities = context.cities.ToList()
        End Using

    End Sub

End Class
